<template>
  <div>
    <!-- width使用百分比, max-width使用具体的1000px , 用以适配小屏幕的PC端 -->
    <el-dialog
      :title="title"
      v-model="visable"
      width="65%"
      :close-on-click-modal="false"
      :draggable="true"
      style="max-width: 850px;"
    >
      <div class="bs-dialog">
        <el-form
          ref="addRef"
          :model="dialogFormData"
          :rules="dialogFormRules"
          label-width="auto"
          style="width: 95%;padding-left: 5%;"
        >
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="行情日期: " prop="date">
                <el-date-picker
                      
                      v-model="dialogFormData.date"
                      type="date"
                      placeholder="请选择行情日期"
                      size="small"
                      value-format="YYYY-MM-DD"
                  />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="合约代码:" prop="instrumentCode">
                <el-input v-model="dialogFormData.instrumentCode" placeholder="请输入合约代码"  clearable />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="最新价:" prop="lastPrice">
                <el-input-number v-model="dialogFormData.lastPrice" placeholder="请输入最新价"  clearable/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="持仓量:" prop="openInterest">
                <el-input-number v-model="dialogFormData.openInterest" placeholder="请输入持仓量"  clearable/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="成交量:" prop="tradeVolume">
                <el-input-number v-model="dialogFormData.tradeVolume" placeholder="请输入成交量"  clearable/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="结算价:" prop="settlementPrice">
                <el-input-number v-model="dialogFormData.settlementPrice" placeholder="请输入结算价"  clearable/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="涨停价:" prop="limitUpPrice">
                <el-input-number v-model="dialogFormData.limitUpPrice" placeholder="请输入涨停价"  clearable/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="跌停价:" prop="limitDownPrice">
                <el-input-number v-model="dialogFormData.limitDownPrice" placeholder="请输入跌停价"  clearable/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="最高价:" prop="topPrice">
                <el-input-number v-model="dialogFormData.topPrice" placeholder="请输入最高价"  clearable/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="最低价:" prop="bottomPrice">
                <el-input-number v-model="dialogFormData.bottomPrice" placeholder="请输入最低价"  clearable/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <template #footer>
        <div class="dialog-footer" style="text-align: right;margin-right: 7%;">
          <el-button  size="small" @click="visable = false"> 取消 </el-button>
          <el-button  size="small" type="primary" @click="save"> 保存 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { updateLastPrice, addLastPrice, getLastPriceByID } from '@/api/oss/lastPrice.js'
// (修改和新增共用一个弹窗时,this.$refs['xxx'].resetFields()函数失效, 因此使用这种方式重置表单数据)
let orgDialogFormData = {
        id: undefined,
        instrumentID:"",
        instrumentCode:"",
        date:"",
        lastPrice:"",
        openInterest:"",
        tradeVolume:"",
        settlementPrice:"",
        limitUpPrice:"",
        limitDownPrice:"",
        maxPrice:"",
        minPrice:"",
      }
export default {
  data() {
    return {
      title:"",
      visable: false,
      dialogFormData: { ...orgDialogFormData},
      dialogFormRules: {
        date: [
          { required: true, message: '请选择行情日期', trigger: ['blur', 'change'] },
        ],
        instrumentCode: [
          { required: true, message: '请输入合约代码', trigger: ['blur', 'change'] },
        ],
        lastPrice: [
          { required: true, message: '请输入最新价', trigger: ['blur', 'change'] },
        ],
      },
    }
  },
  methods: {
    openModal(id) {
      console.log("openModal=",id);
      this.visable = true
      this.$nextTick(() => {
        this.dialogFormData = {...orgDialogFormData}
        if (this.isNotBlank(id)) {
          this.title = '修改行情信息'
          // 获取修改的信息
          getLastPriceByID({ 'id': id }).then(res => {
            console.log("修改返回值=",res);
            if (res.code === 1) {
              this.dialogFormData = {...res.data}
            }
          })
        } else {
          this.title = '新增行情信息'
        }
      })
    },
    
    // 保存
    save() {
      this.$refs['addRef'].validate((valid) => {
        if (valid) {
          if (this.isBlank(this.dialogFormData.id)) {
            addLastPrice(this.dialogFormData).then(res => {
              if (res.code == 1) {
                this.$message.success('操作成功')
                this.visable = false
                // 刷新父组件中的表格数据
                this.$emit('flushTable', {})
              } else {
                this.$message.error(res.msg)
              }
            }).catch((error) => {
            })
          } else {
            updateLastPrice(this.dialogFormData).then(res => {
              if (res.code == 1) {
                this.$message.success('操作成功')
                this.visable = false
                // 刷新父组件中的表格数据
                this.$emit('flushTable', {})
              } else {
                this.$message.error(res.msg)
              }
            }).catch((error) => {
            })
          }
          

        }
      })
    },

  }
}
</script>
<style scoped>
  :deep(.el-form-item__content) {
    display: inline-block;
    text-align: left;
    padding-right: 10%;
  }
  :deep(.el-drawer__title) {
    text-align: left;
  }
  :deep(.el-input),
  :deep(.el-input-number--small) {
    width: 100%;
  }
</style>
